package com.teradata.tool.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.teradata.tool.engine.GenDD;
import com.teradata.tool.xml.bean.DdBean;
import com.teradata.tool.xml.util.XmlUtil;

public class DdDialogUI extends JDialog {

	private final JPanel		contentPanel	= new JPanel();
	private JTextField			tFidSourceDir;
	private JTextField			tFddDir;
	private JTextField			tFdataDeli;
	private JTextField			tFprefix;
	private JTextField			tFsuffix;
	private JTextField			tFsourceDb;
	private JTextField			tFsourceTable;
	private JTextField			tFidNameWildWord;
	private final DdBean		ddBean;
	private final XmlUtil		ddBeanUtil;
	private final JFileChooser	fc				= new JFileChooser();
	private JTextField			tFsystemCode;
	private JTextField			tFfileDeli;
	private JTextField			tFtableOverview;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DdDialogUI dialog = new DdDialogUI();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DdDialogUI() {
		setTitle("Data Dictionary");
		setBounds(100, 100, 637, 411);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		ddBean = new DdBean();
		ddBeanUtil = new XmlUtil(ddBean);
		ddBeanUtil.getIndexOfBean(0);

		JLabel lidSourceDir = new JLabel("\u7B80\u8981\u6570\u636E\u6620\u5C04\u76EE\u5F55:");
		lidSourceDir.setBounds(10, 11, 120, 22);
		contentPanel.add(lidSourceDir);

		tFidSourceDir = new JTextField();
		tFidSourceDir.setBounds(119, 12, 378, 20);
		tFidSourceDir.setText(ddBean.getIdMappingDir());
		contentPanel.add(tFidSourceDir);
		tFidSourceDir.setColumns(10);

		JButton btnidSourceDir = new JButton("\u6D4F\u89C8");
		btnidSourceDir.setBounds(507, 11, 89, 23);
		btnidSourceDir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lookUp("btnidSourceDir");
			}
		});
		contentPanel.add(btnidSourceDir);

		JLabel lddDir = new JLabel("\u6570\u636E\u5B57\u5178\u5B58\u653E\u76EE\u5F55\uFF1A");
		lddDir.setBounds(10, 44, 120, 14);
		contentPanel.add(lddDir);

		tFddDir = new JTextField();
		tFddDir.setBounds(119, 38, 379, 20);
		tFddDir.setText(ddBean.getDdDir());
		contentPanel.add(tFddDir);
		tFddDir.setColumns(10);

		JButton btnddDir = new JButton("\u6D4F\u89C8");
		btnddDir.setBounds(507, 40, 89, 23);
		btnddDir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lookUp("btnddDir");
			}
		});
		contentPanel.add(btnddDir);

		JLabel ldataFixOrVar = new JLabel("\u5B9A\u53D8\u957F\u6807\u5FD7\uFF1A");
		ldataFixOrVar.setBounds(10, 71, 120, 14);
		contentPanel.add(ldataFixOrVar);

		final JComboBox cbBdataFixOrVar = new JComboBox();
		cbBdataFixOrVar.addItem("Y");
		cbBdataFixOrVar.addItem("N");
		cbBdataFixOrVar.setSelectedIndex(0);
		cbBdataFixOrVar.setSelectedIndex((ddBean.getDataFixOrVar().equalsIgnoreCase("Y")) ? 0 : 1);
		cbBdataFixOrVar.setBounds(119, 69, 113, 20);
		contentPanel.add(cbBdataFixOrVar);

		JLabel ldataDeli = new JLabel("\u53D8\u957F\u5206\u9694\u7B26:");
		ldataDeli.setBounds(335, 71, 100, 14);
		contentPanel.add(ldataDeli);

		tFdataDeli = new JTextField();
		tFdataDeli.setBounds(418, 69, 176, 20);
		tFdataDeli.setText(ddBean.getDataDeli());
		contentPanel.add(tFdataDeli);
		tFdataDeli.setColumns(10);

		JLabel lunsigndec = new JLabel("\u6570\u503C\u662F\u5426\u5E26\u7B26\u53F7\uFF1A");
		lunsigndec.setBounds(10, 96, 120, 14);
		contentPanel.add(lunsigndec);

		final JComboBox cbBunsigndec = new JComboBox();
		cbBunsigndec.addItem("Y");
		cbBunsigndec.addItem("N");
		cbBunsigndec.setSelectedIndex(0);
		cbBunsigndec.setBounds(119, 93, 113, 20);
		cbBunsigndec.setSelectedIndex((ddBean.getUnsignDecFlag().equalsIgnoreCase("Y")) ? 0 : 1);
		contentPanel.add(cbBunsigndec);

		JLabel lblprefix = new JLabel("\u63A5\u53E3\u540D\u524D\u7F00:");
		lblprefix.setBounds(335, 96, 100, 14);
		contentPanel.add(lblprefix);

		tFprefix = new JTextField();
		tFprefix.setBounds(418, 93, 176, 20);
		tFprefix.setText(ddBean.getPrefixString());
		contentPanel.add(tFprefix);
		tFprefix.setColumns(10);

		JLabel lblsuffix = new JLabel("\u63A5\u53E3\u540D\u540E\u7F00:");
		lblsuffix.setBounds(335, 124, 89, 14);
		contentPanel.add(lblsuffix);

		tFsuffix = new JTextField();
		tFsuffix.setBounds(418, 121, 176, 20);
		tFsuffix.setText(ddBean.getSuffixString());
		contentPanel.add(tFsuffix);
		tFsuffix.setColumns(10);

		JLabel lblexcelOrTableFlag = new JLabel("\u6765\u6E90\u9009\u62E9\uFF1A");
		lblexcelOrTableFlag.setBounds(10, 121, 89, 14);
		contentPanel.add(lblexcelOrTableFlag);

		final JComboBox cbbexcelOrTableFlag = new JComboBox();
		cbbexcelOrTableFlag.addItem("TABLE");
		cbbexcelOrTableFlag.addItem("EXCEL");
		cbbexcelOrTableFlag.setSelectedIndex(0);
		cbbexcelOrTableFlag.setSelectedIndex((ddBean.getExcelOrTableFlag().equalsIgnoreCase("TABLE")) ? 0 : 1);
		cbbexcelOrTableFlag.setBounds(119, 121, 113, 20);
		contentPanel.add(cbbexcelOrTableFlag);

		JLabel lblsourceDb = new JLabel("\u6765\u6E90\u5E93\u540D:");
		lblsourceDb.setBounds(335, 173, 89, 17);
		contentPanel.add(lblsourceDb);

		tFsourceDb = new JTextField();
		tFsourceDb.setBounds(418, 173, 178, 20);
		tFsourceDb.setText(ddBean.getSourceDb());
		contentPanel.add(tFsourceDb);
		tFsourceDb.setColumns(10);

		JLabel llblSourceTable = new JLabel("\u6765\u6E90\u8868\u540D\uFF1A");
		llblSourceTable.setBounds(335, 149, 70, 14);
		contentPanel.add(llblSourceTable);

		tFsourceTable = new JTextField();
		tFsourceTable.setBounds(418, 148, 176, 20);
		tFsourceTable.setText(ddBean.getSourceDb());
		contentPanel.add(tFsourceTable);
		tFsourceTable.setColumns(10);

		JLabel lblIDwildword = new JLabel("ID\u6587\u4EF6\u540D\u901A\u914D\u7B26:");
		lblIDwildword.setBounds(10, 173, 100, 14);
		contentPanel.add(lblIDwildword);

		tFidNameWildWord = new JTextField();
		tFidNameWildWord.setBounds(119, 173, 176, 20);
		tFidNameWildWord.setText(ddBean.getIdNameWildWord());
		contentPanel.add(tFidNameWildWord);
		tFidNameWildWord.setColumns(10);

		JLabel lblcdcflag = new JLabel("CDC\u6807\u5FD7\uFF1A");
		lblcdcflag.setBounds(10, 149, 89, 14);
		contentPanel.add(lblcdcflag);

		final JComboBox cbbCdcFlag = new JComboBox();
		cbbCdcFlag.addItem("Y");
		cbbCdcFlag.addItem("N");
		cbbCdcFlag.setSelectedIndex(0);
		cbbCdcFlag.setBounds(119, 146, 113, 20);
		cbbCdcFlag.setSelectedIndex((ddBean.getCdcTableFlag().equalsIgnoreCase("Y")) ? 0 : 1);

		contentPanel.add(cbbCdcFlag);

		JLabel lblSystemCode = new JLabel("\u7CFB\u7EDF\u4EE3\u7801\uFF1A");
		lblSystemCode.setBounds(10, 196, 89, 14);
		contentPanel.add(lblSystemCode);

		tFsystemCode = new JTextField();
		tFsystemCode.setBounds(119, 196, 176, 20);
		tFsystemCode.setText(ddBean.getSysCode());
		contentPanel.add(tFsystemCode);
		tFsystemCode.setColumns(10);

		JLabel lblFiledeli = new JLabel("\u6587\u4EF6\u540D\u5206\u9694\u7B26:");
		lblFiledeli.setBounds(335, 201, 70, 14);
		contentPanel.add(lblFiledeli);

		tFfileDeli = new JTextField();
		tFfileDeli.setBounds(418, 196, 178, 20);
		contentPanel.add(tFfileDeli);
		tFfileDeli.setColumns(10);

		JLabel lblTableoverview = new JLabel("\u8868\u6982\u51B5:");
		lblTableoverview.setBounds(10, 221, 89, 14);
		contentPanel.add(lblTableoverview);

		tFtableOverview = new JTextField();
		tFtableOverview.setBounds(119, 218, 176, 20);
		tFtableOverview.setText(ddBean.getTableOverview());
		contentPanel.add(tFtableOverview);
		tFtableOverview.setColumns(10);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				final JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						okButton.setVisible(true);

						ddBean.setIdMappingDir(tFidSourceDir.getText());
						ddBean.setDdDir(tFddDir.getText());
						ddBean.setSourceDb(tFsourceDb.getText());
						ddBean.setIdMappingAll(tFsourceTable.getText());
						ddBean.setDataFixOrVar(cbBdataFixOrVar.getSelectedItem().toString());
						ddBean.setCdcTableFlag(cbbCdcFlag.getSelectedItem().toString());
						ddBean.setDataDeli(tFdataDeli.getText());
						ddBean.setExcelOrTableFlag(cbbexcelOrTableFlag.getSelectedItem().toString());
						ddBean.setIdNameWildWord(tFidNameWildWord.getText());
						ddBean.setPrefixString(tFprefix.getText());
						ddBean.setUnsignDecFlag(cbBunsigndec.getSelectedItem().toString());
						ddBean.setSysCode(tFsystemCode.getText());
						ddBean.setSuffixString(tFsuffix.getText());
						ddBean.setFiledeli(tFfileDeli.getText());
						ddBean.setTableOverview(tFtableOverview.getText());
						ddBeanUtil.modifyMsg();

						GenDD genDd = new GenDD(ddBean.getDdDir(), ddBean.getSysCode(), ddBean.getIdMappingDir(),
								ddBean.getIdNameWildWord(), ddBean.getExcelOrTableFlag(), ddBean.getFiledeli(), ddBean
										.getSuffixString(), ddBean.getUnsignDecFlag(), ddBean.getDataFixOrVar(), ddBean
										.getDataDeli(), ddBean.getCdcTableFlag(), ddBean.getTableOverview(), ddBean
										.getIdMappingAll(), ddBean.getSourceDb(), ddBean.getPrefixString());
						try {
							genDd.run();
							dispose();
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					}
				});

				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				final JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						cancelButton.setVisible(true);
						dispose();
					}
				});
				buttonPane.add(cancelButton);
			}
		}
	}

	private void lookUp(String buttonName) {
		// tFpath, tFfpath, tFmpath, tFf1model, tFf1mode2, tFspath, tFchange;
		if (buttonName.equals("btnidSourceDir")) {
			fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			int intRetVal = fc.showOpenDialog(this);
			if (intRetVal == JFileChooser.APPROVE_OPTION) {
				tFidSourceDir.setText(fc.getSelectedFile().getPath().replace("\\", "/"));
			}
		}
		if (buttonName.equals("btnddDir")) {
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int intRetVal1 = fc.showOpenDialog(this);
			if (intRetVal1 == JFileChooser.APPROVE_OPTION) {
				tFddDir.setText(fc.getSelectedFile().getPath().replace("\\", "/"));
			}
		}

	}
}
